#!/usr/bin/env python
# -*- coding: utf-8 -*-

# @Time    : 2021/3/8 21:49
# @Author  : Keyman
# @File    : collections.py

from abc import ABCMeta, abstractmethod


class Sequence(metaclass=ABCMeta):
    """Our own version of collections.Sequence abstract base class."""

    @abstractmethod
    def __len__(self):
        """Return the length of the sequence."""

    @abstractmethod
    def __getitem__(self, j):
        """Return the element at index j of the sequence."""

    @abstractmethod
    def __contains__(self, val):
        """
        仿照内置的写法
        :param val: the value which need to be found
        :return: True if val found; False otherwise
        """
        for v in self:
            if v is val or v == val:
                return True
        return False

    def index(self, val):
        """
        Return leftmost index at which val is found (or raise ValueError).
        """
        for v in range(len(self)):
            try:
                if self[v] is val or self[v] == val:
                    return v
            except IndexError:
                break
        return ValueError('value not in sequence')

    def count(self, val):
        """Return the number of elements equal to given value."""
        count = 0
        for v in self:
            if v is val or v == val:
                count += 1
        return count
